// import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:untitled2/pages/widget/MyDialog.dart';
import 'package:date_format/date_format.dart';
import 'dart:convert';

class MessagePage extends StatefulWidget {
  const MessagePage({Key? key}) : super(key: key);

  @override
  State<MessagePage> createState() => _MessagePageState();
}

class _MessagePageState extends State<MessagePage> {
  void showMessageDilalog(Function(String) callback) {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(title: const Text("提示"), content: const Text("确定删除吗？"), actions: [
            TextButton(
                onPressed: () {
                  Navigator.of(context).pop("取消");
                  callback("取消");
                },
                child: const Text("取消")),
            TextButton(
                onPressed: () {
                  Navigator.of(context).pop("确定");
                  callback("确定");
                },
                child: const Text("确定"))
          ]);
        });
  }

  void showSimpDilalog(Function(String) callback) {
    showDialog(
        barrierDismissible: false, //触摸背景，对话框消失
        context: context,
        builder: (context) {
          return SimpleDialog(title: const Text("提示"), children: [
            TextButton(
                onPressed: () {
                  Navigator.of(context).pop("取消");
                  callback("取消");
                },
                child: const Text("取消")),
            TextButton(
                onPressed: () {
                  Navigator.of(context).pop("确定");
                  callback("确定");
                  Fluttertoast.showToast(
                    msg: "点击了确定",
                    toastLength: Toast.LENGTH_SHORT,
                    gravity: ToastGravity.CENTER,
                    timeInSecForIosWeb: 1,
                    backgroundColor: Colors.black,
                    textColor: Colors.white,
                    fontSize: 16.0,
                  );
                },
                child: const Text("确定"))
          ]);
        });
  }

  void showModalDilalog(Function(String) callback) {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return SizedBox(
            height: 120,
            child: Center(
                child: Column(
              children: [
                TextButton(
                    onPressed: () {
                      Navigator.of(context).pop("取消");
                      callback("取消");
                    },
                    child: const Text("取消")),
                Divider(),
                TextButton(
                    onPressed: () {
                      Navigator.of(context).pop("确定");
                      callback("确定");
                    },
                    child: const Text("确定"))
              ],
            )),
          );
        });
  }

  void showMyDialog(Function(String) callback) {
    showDialog(
        context: context,
        barrierDismissible: true, //触摸背景，对话框消失
        builder: (context) {
          return MyDialog(
            title: "通知",
            content: "你收到一个新的消息",
            okCallback: () {
              callback("点击了确认");
            },
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          SizedBox(
            height: 100,
            child: Row(
              children: [
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.red,
                    alignment: Alignment.center,
                    child: const Text("跳转到Class", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/oneClass");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.blue,
                    alignment: Alignment.center,
                    child: const Text(
                      "弹出框",
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  onTap: () {
                    showSimpDilalog((res) {
                      print(res);
                    });
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  onTap: () {
                    showMessageDilalog((result) {
                      print(result);
                    });
                  },
                  child: Container(
                    height: 100,
                    color: Colors.black,
                    alignment: Alignment.center,
                    child: const Text("弹出框2", style: TextStyle(color: Colors.white)),
                  ),
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.yellow,
                    alignment: Alignment.center,
                    child: const Text("底部弹窗", style: TextStyle(color: Colors.black)),
                  ),
                  onTap: () {
                    showModalDilalog((res) {
                      print(res);
                    });
                  },
                )),
              ],
            ),
          ),
          SizedBox(
            height: 100,
            child: Row(
              children: [
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.yellow,
                    alignment: Alignment.center,
                    child: const Text("自定义弹窗", style: TextStyle(color: Colors.black)),
                  ),
                  onTap: () {
                    showMyDialog((res) {
                      Fluttertoast.showToast(msg: res);
                    });
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.deepPurple,
                    alignment: Alignment.center,
                    child: const Text("抖音视频无线滑动", textAlign: TextAlign.center, style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/douyin");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.purple,
                    alignment: Alignment.center,
                    child: const Text("banner", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/banner");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.blue,
                    alignment: Alignment.center,
                    child: const Text("时间", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    var dateTime = DateTime.now();
                    print(dateTime.millisecondsSinceEpoch);
                    print(DateTime.fromMillisecondsSinceEpoch(dateTime.millisecondsSinceEpoch));
                    // 格式化时间
                    print(formatDate(DateTime.now(), [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ':', ss]));
                    Navigator.of(context).pushNamed("/datePicker");
                  },
                )),
              ],
            ),
          ),
          SizedBox(
            height: 100,
            child: Row(
              children: [
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.red,
                    alignment: Alignment.center,
                    child: const Text("JSON", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Map mapData = {"name": "张三", "age": "20"};
                    var strData = '{"name":"张三","age":"20"}';
                    print(json.encode(mapData));
                    print(json.decode(strData));
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.blue,
                    alignment: Alignment.center,
                    child: const Text("新闻", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/News");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.black,
                    alignment: Alignment.center,
                    child: const Text("设备信息", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/DeviceInfo");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.purple,
                    alignment: Alignment.center,
                    child: const Text("高德地图", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/GaodeLocation");
                  },
                )),
              ],
            ),
          ),
          SizedBox(
            height: 100,
            child: Row(
              children: [
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.blue,
                    alignment: Alignment.center,
                    child: const Text("视频播放器", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/VideoPlay");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.red,
                    alignment: Alignment.center,
                    child: const Text("扫一扫", style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/MobileScanner");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.yellow,
                    alignment: Alignment.center,
                    child: const Text("版本更新", style: TextStyle(color: Colors.red)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/UpDataApp");
                  },
                )),
                Expanded(
                    child: GestureDetector(
                  child: Container(
                    height: 100,
                    color: Colors.cyan,
                    alignment: Alignment.center,
                    child: const Text("跳转到外部应用", textAlign: TextAlign.center, style: TextStyle(color: Colors.white)),
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed("/JumpApp");
                  },
                )),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
